package com.autoglm.data.local.dao

import androidx.room.*
import com.autoglm.data.local.entity.UserPreferenceEntity
import kotlinx.coroutines.flow.Flow

/**
 * 用户偏好数据访问对象
 */
@Dao
interface UserPreferenceDao {
    
    @Query("SELECT * FROM user_preferences")
    fun getAllPreferences(): Flow<List<UserPreferenceEntity>>
    
    @Query("SELECT * FROM user_preferences WHERE key = :key")
    suspend fun getPreferenceByKey(key: String): UserPreferenceEntity?
    
    @Query("SELECT * FROM user_preferences WHERE key = :key")
    fun getPreferenceByKeyFlow(key: String): Flow<UserPreferenceEntity?>
    
    @Query("SELECT * FROM user_preferences WHERE type = :type")
    fun getPreferencesByType(type: String): Flow<List<UserPreferenceEntity>>
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertPreference(preference: UserPreferenceEntity)
    
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertPreferences(preferences: List<UserPreferenceEntity>)
    
    @Update
    suspend fun updatePreference(preference: UserPreferenceEntity)
    
    @Delete
    suspend fun deletePreference(preference: UserPreferenceEntity)
    
    @Query("DELETE FROM user_preferences WHERE key = :key")
    suspend fun deletePreferenceByKey(key: String)
    
    @Query("DELETE FROM user_preferences WHERE type = :type")
    suspend fun deletePreferencesByType(type: String)
    
    @Query("DELETE FROM user_preferences")
    suspend fun deleteAllPreferences()
}
